// 创建Vuex核心 store

// 第一步引入Vuex
import Vuex from 'vuex'

// 引入Vue
import Vue from 'vue'

// 使用Vuex
Vue.use(Vuex)


// 暴露store 使其他VueComponents可以使用   简称vc组件实例对象
const store = new Vuex.Store({
    //公共state对象
    state: {
        chatMsg: {
            code: '',
            msg: ''
        },
        addFriendMsg: {
            code: '',
            msg: ''
        },
        groupMsg: {
            code: '',
            msg: '',
            userId: ''
        }
    },
    //修改state值方法
    mutations: {
        // 聊天消息
        set_msg(state, payload) {
            state.chatMsg = payload
        },
        // 添加好友消息
        set_addFriendMsg(state, payload) {
            state.addFriendMsg = payload
        },
        //群组消息通知
        set_groupMsg(state, payload) {
            state.groupMsg = payload
        }
    },
    //取值方法
    getters: {
        getChatMsg(state) {
            return state.chatMsg
        },
        getAddFriendMsg(state) {
            return state.addFriendMsg
        },
        getGroupMsg(state) {
            return state.groupMsg
        }
    },
    //异步调用mutations
    actions: {
        asyncUpdateChatMsg(context, chatMsg) {
            context.commit('set_msg', chatMsg)
        },
        asyncUpdateAddFriendMsg(context, addFriendMsg) {
            context.commit('set_addFriendMsg', addFriendMsg)
        },
        asyncUpdateGroupMsg(context, groupMsg) {
            context.commit('set_groupMsg', groupMsg)
        }
    },
    modules: {}
})

export default store